package com.meritdata.cloud.data.service.development.controller;

import com.meritdata.cloud.data.manager.model.repository.DataModelRepository;
import com.meritdata.cloud.data.manager.model.service.IDataModelService;
import com.meritdata.cloud.data.service.development.model.TdDataApiParameter;
import com.meritdata.cloud.data.service.development.model.TdDataApiTable;
import com.meritdata.cloud.data.service.development.service.DataApiParameterService;
import com.meritdata.cloud.data.service.development.service.DataApiTableService;
import com.meritdata.cloud.resultmodel.ResultBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@RequestMapping("/apiTable")
@Controller
public class DataApiTableController {

    @Autowired
    private DataApiTableService service;

    @Autowired
    private IDataModelService dataModelService;

    @Autowired
    private DataModelRepository dataModelRepository;

    @RequestMapping("/create")
    @ResponseBody
    public ResultBody create(@RequestParam String apiId,@RequestParam String tableId ) {
        ResultBody r = new ResultBody();
        service.deleteByapiId(Long.valueOf(apiId));
        TdDataApiTable d = new TdDataApiTable();
        d.setApiId(Long.valueOf(apiId));
        d.setTableId(Long.valueOf(tableId));
        d.setUpdateTime(new Date());
        d.setCreateTime(new Date());
        service.save(d);
        r.setData(d);
        r.setSuccess(true);
        return r;
    }
    @RequestMapping("/update")
    @ResponseBody
    public ResultBody update( @RequestParam String id,@RequestParam String tableId ) {
        ResultBody r = new ResultBody();
        r.setData(service.udpate(id,tableId,new Date()));
        r.setSuccess(true);
        return r;
    }
    @RequestMapping("/findByApiId")
    @ResponseBody
    public ResultBody findByApiId( @RequestParam String apiId ) {
        ResultBody r = new ResultBody();
        TdDataApiTable t = service.findByApiId(Long.valueOf(apiId));
        r.setData(dataModelRepository.findById(t.getTableId()));
        r.setSuccess(true);
        return r;
    }
}